﻿
@inject IHttpService _httpService
@inject IPopupService PopupService

<MButton Text Block Color="secondary" OnClick="OnHidden">
    <MIcon Left>@(IsHidden? "mdi-eye " : "mdi-eye-off")</MIcon>
    @(IsHidden?("取消隐藏" + Type.GetDisplayName()):("隐藏"+Type.GetDisplayName()))
</MButton>

@code {
    [Parameter]
    public bool IsHidden { get; set; }
    [Parameter]
    public long Id { get; set; }
    [Parameter]
    public ExaminedNormalListModelType Type { get; set; }
    [Parameter]
    public EventCallback<bool> IsHiddenChanged { get; set; }

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    private async void OnHidden()
    {
        var str = (IsHidden ? "显示" : "隐藏") + Type.GetDisplayName();

        try
        {
            var url = Type switch
            {
                ExaminedNormalListModelType.Article => "api/articles/Hide",
                ExaminedNormalListModelType.Entry => "api/entries/HiddenEntry",
                ExaminedNormalListModelType.Periphery => "api/peripheries/HiddenPeriphery",
                ExaminedNormalListModelType.Tag => "api/tags/HiddenTag",
                ExaminedNormalListModelType.Video => "api/videos/Hide"
            };


            var obj = await _httpService.PostAsync<HiddenArticleModel, Result>( url, new HiddenArticleModel { Ids = new long[] { Id }, IsHidden = !IsHidden });
            //判断结果
            if (obj.Successful == false)
            {
                await PopupService.ToastErrorAsync(str + "失败", obj.Error);
            }
            else
            {
                await PopupService.ToastSuccessAsync(str + "成功", str + "成功，离开此页面后可以请在后台恢复状态");
                IsHidden = !IsHidden;
                await IsHiddenChanged.InvokeAsync(IsHidden);
                StateHasChanged();
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, str + "失败");
        }
    }

}
